home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / win32com / client / __init__.pyc (.txt) next >
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  18.4 KB  |  401 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. import __builtin__
  5. NeedUnicodeConversions = not hasattr(__builtin__, 'unicode')
  6. import dynamic
  7. import gencache
  8. import pythoncom
  9. import sys
  10. import pywintypes
  11.  
  12. def __WrapDispatch(dispatch, userName = None, resultCLSID = None, typeinfo = None, UnicodeToString = NeedUnicodeConversions, clsctx = pythoncom.CLSCTX_SERVER, WrapperClass = None):
  13.     '''
  14.     Helper function to return a makepy generated class for a CLSID if it exists,
  15.     otherwise cope by using CDispatch.
  16.   '''
  17.     if resultCLSID is None:
  18.         
  19.         try:
  20.             typeinfo = dispatch.GetTypeInfo()
  21.             if typeinfo is not None:
  22.                 resultCLSID = str(typeinfo.GetTypeAttr()[0])
  23.         except pythoncom.com_error:
  24.             pass
  25.  
  26.     
  27.     if resultCLSID is not None:
  28.         import gencache
  29.         klass = gencache.GetClassForCLSID(resultCLSID)
  30.         if klass is not None:
  31.             return klass(dispatch)
  32.         
  33.     
  34.     if WrapperClass is None:
  35.         WrapperClass = CDispatch
  36.     
  37.     return dynamic.Dispatch(dispatch, userName, WrapperClass, typeinfo, UnicodeToString = UnicodeToString, clsctx = clsctx)
  38.  
  39.  
  40. def GetObject(Pathname = None, Class = None, clsctx = None):
  41.     '''
  42.     Mimic VB\'s GetObject() function.
  43.  
  44.     ob = GetObject(Class = "ProgID") or GetObject(Class = clsid) will
  45.     connect to an already running instance of the COM object.
  46.     
  47.     ob = GetObject(r"c:\x08lah\x08lah\x0coo.xls") (aka the COM moniker syntax)
  48.     will return a ready to use Python wrapping of the required COM object.
  49.  
  50.     Note: You must specifiy one or the other of these arguments. I know
  51.     this isn\'t pretty, but it is what VB does. Blech. If you don\'t
  52.     I\'ll throw ValueError at you. :)
  53.     
  54.     This will most likely throw pythoncom.com_error if anything fails.
  55.   '''
  56.     if clsctx is None:
  57.         clsctx = pythoncom.CLSCTX_ALL
  58.     
  59.     if Pathname is None and Class is None and Pathname is not None and Class is not None:
  60.         raise ValueError, 'You must specify a value for Pathname or Class, but not both.'
  61.     
  62.     if Class is not None:
  63.         return GetActiveObject(Class, clsctx)
  64.     else:
  65.         return Moniker(Pathname, clsctx)
  66.  
  67.  
  68. def GetActiveObject(Class, clsctx = pythoncom.CLSCTX_ALL):
  69.     """
  70.     Python friendly version of GetObject's ProgID/CLSID functionality.
  71.   """
  72.     resultCLSID = pywintypes.IID(Class)
  73.     dispatch = pythoncom.GetActiveObject(resultCLSID)
  74.     dispatch = dispatch.QueryInterface(pythoncom.IID_IDispatch)
  75.     return __WrapDispatch(dispatch, Class, resultCLSID = resultCLSID, clsctx = clsctx)
  76.  
  77.  
  78. def Moniker(Pathname, clsctx = pythoncom.CLSCTX_ALL):
  79.     """
  80.     Python friendly version of GetObject's moniker functionality.
  81.   """
  82.     (moniker, i, bindCtx) = pythoncom.MkParseDisplayName(Pathname)
  83.     dispatch = moniker.BindToObject(bindCtx, None, pythoncom.IID_IDispatch)
  84.     return __WrapDispatch(dispatch, Pathname, clsctx = clsctx)
  85.  
  86.  
  87. def Dispatch(dispatch, userName = None, resultCLSID = None, typeinfo = None, UnicodeToString = NeedUnicodeConversions, clsctx = pythoncom.CLSCTX_SERVER):
  88.     '''Creates a Dispatch based COM object.
  89.   '''
  90.     (dispatch, userName) = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
  91.     return __WrapDispatch(dispatch, userName, resultCLSID, typeinfo, UnicodeToString, clsctx)
  92.  
  93.  
  94. def DispatchEx(clsid, machine = None, userName = None, resultCLSID = None, typeinfo = None, UnicodeToString = NeedUnicodeConversions, clsctx = None):
  95.     '''Creates a Dispatch based COM object on a specific machine.
  96.   '''
  97.     if clsctx is None:
  98.         clsctx = pythoncom.CLSCTX_SERVER
  99.         if machine is not None:
  100.             clsctx = clsctx & ~(pythoncom.CLSCTX_INPROC)
  101.         
  102.     
  103.     if machine is None:
  104.         serverInfo = None
  105.     else:
  106.         serverInfo = (machine,)
  107.     if userName is None:
  108.         userName = clsid
  109.     
  110.     dispatch = pythoncom.CoCreateInstanceEx(clsid, None, clsctx, serverInfo, (pythoncom.IID_IDispatch,))[0]
  111.     return Dispatch(dispatch, userName, resultCLSID, typeinfo, UnicodeToString = UnicodeToString, clsctx = clsctx)
  112.  
  113.  
  114. class CDispatch(dynamic.CDispatch):
  115.     '''
  116.     The dynamic class used as a last resort.
  117.     The purpose of this overriding of dynamic.CDispatch is to perpetuate the policy
  118.     of using the makepy generated wrapper Python class instead of dynamic.CDispatch
  119.     if/when possible.
  120.   '''
  121.     
  122.     def _wrap_dispatch_(self, ob, userName = None, returnCLSID = None, UnicodeToString = NeedUnicodeConversions):
  123.         return Dispatch(ob, userName, returnCLSID, None, UnicodeToString)
  124.  
  125.  
  126.  
  127. class Constants:
  128.     '''A container for generated COM constants.
  129.   '''
  130.     
  131.     def __init__(self):
  132.         self.__dicts__ = []
  133.  
  134.     
  135.     def __getattr__(self, a):
  136.         for d in self.__dicts__:
  137.             if d.has_key(a):
  138.                 return d[a]
  139.             
  140.         
  141.         raise AttributeError, a
  142.  
  143.  
  144. constants = Constants()
  145.  
  146. def _event_setattr_(self, attr, val):
  147.     
  148.     try:
  149.         self.__class__.__bases__[0].__setattr__(self, attr, val)
  150.     except AttributeError:
  151.         self.__dict__[attr] = val
  152.  
  153.  
  154.  
  155. class EventsProxy:
  156.     
  157.     def __init__(self, ob):
  158.         self.__dict__['_obj_'] = ob
  159.  
  160.     
  161.     def __del__(self):
  162.         
  163.         try:
  164.             self._obj_.close()
  165.         except pythoncom.com_error:
  166.             pass
  167.  
  168.  
  169.     
  170.     def __getattr__(self, attr):
  171.         return getattr(self._obj_, attr)
  172.  
  173.     
  174.     def __setattr__(self, attr, val):
  175.         setattr(self._obj_, attr, val)
  176.  
  177.  
  178.  
  179. def DispatchWithEvents(clsid, user_event_class):
  180.     '''Create a COM object that can fire events to a user defined class.
  181.   clsid -- The ProgID or CLSID of the object to create.
  182.   user_event_class -- A Python class object that responds to the events.
  183.  
  184.   This requires makepy support for the COM object being created.  If
  185.   this support does not exist it will be automatically generated by
  186.   this function.  If the object does not support makepy, a TypeError
  187.   exception will be raised.
  188.  
  189.   The result is a class instance that both represents the COM object
  190.   and handles events from the COM object.
  191.  
  192.   It is important to note that the returned instance is not a direct
  193.   instance of the user_event_class, but an instance of a temporary
  194.   class object that derives from three classes:
  195.   * The makepy generated class for the COM object
  196.   * The makepy generated class for the COM events
  197.   * The user_event_class as passed to this function.
  198.  
  199.   If this is not suitable, see the getevents function for an alternative
  200.   technique of handling events.
  201.  
  202.   Object Lifetimes:  Whenever the object returned from this function is
  203.   cleaned-up by Python, the events will be disconnected from
  204.   the COM object.  This is almost always what should happen,
  205.   but see the documentation for getevents() for more details.
  206.  
  207.   Example:
  208.  
  209.   >>> class IEEvents:
  210.   ...    def OnVisible(self, visible):
  211.   ...       print "Visible changed:", visible
  212.   ...
  213.   >>> ie = DispatchWithEvents("InternetExplorer.Application", IEEvents)
  214.   >>> ie.Visible = 1
  215.   Visible changed: 1
  216.   >>> 
  217.   '''
  218.     disp = Dispatch(clsid)
  219.     if not disp.__dict__.get('CLSID'):
  220.         
  221.         try:
  222.             ti = disp._oleobj_.GetTypeInfo()
  223.             disp_clsid = ti.GetTypeAttr()[0]
  224.             (tlb, index) = ti.GetContainingTypeLib()
  225.             tla = tlb.GetLibAttr()
  226.             mod = gencache.EnsureModule(tla[0], tla[1], tla[3], tla[4])
  227.             disp_class = gencache.GetClassForProgID(str(disp_clsid))
  228.         except pythoncom.com_error:
  229.             raise TypeError, 'This COM object can not automate the makepy process - please run makepy manually for this object'
  230.  
  231.     else:
  232.         disp_class = disp.__class__
  233.     import new
  234.     events_class = getevents(clsid)
  235.     result_class = new.classobj('COMEventClass', (disp_class, events_class, user_event_class), {
  236.         '__setattr__': _event_setattr_ })
  237.     instance = result_class(disp._oleobj_)
  238.     events_class.__init__(instance, instance)
  239.     if hasattr(user_event_class, '__init__'):
  240.         user_event_class.__init__(instance)
  241.     
  242.     return EventsProxy(instance)
  243.  
  244.  
  245. def getevents(clsid):
  246.     '''Determine the default outgoing interface for a class, given
  247.     either a clsid or progid. It returns a class - you can
  248.     conveniently derive your own handler from this class and implement
  249.     the appropriate methods.
  250.  
  251.     This method relies on the classes produced by makepy. You must use
  252.     either makepy or the gencache module to ensure that the
  253.     appropriate support classes have been generated for the com server
  254.     that you will be handling events from.
  255.  
  256.     Beware of COM circular references.  When the Events class is connected
  257.     to the COM object, the COM object itself keeps a reference to the Python
  258.     events class.  Thus, neither the Events instance or the COM object will
  259.     ever die by themselves.  The \'close\' method on the events instance
  260.     must be called to break this chain and allow standard Python collection
  261.     rules to manage object lifetimes.  Note that DispatchWithEvents() does
  262.     work around this problem by the use of a proxy object, but if you use
  263.     the getevents() function yourself, you must make your own arrangements
  264.     to manage this circular reference issue.
  265.  
  266.     Beware of creating Python circular references: this will happen if your
  267.     handler has a reference to an object that has a reference back to
  268.     the event source. Call the \'close\' method to break the chain.
  269.     
  270.     Example:
  271.  
  272.     >>>win32com.client.gencache.EnsureModule(\'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}\',0,1,1)
  273.     <module \'win32com.gen_py.....
  274.     >>>
  275.     >>> class InternetExplorerEvents(win32com.client.getevents("InternetExplorer.Application.1")):
  276.     ...    def OnVisible(self, Visible):
  277.     ...        print "Visibility changed: ", Visible
  278.     ...
  279.     >>>
  280.     >>> ie=win32com.client.Dispatch("InternetExplorer.Application.1")
  281.     >>> events=InternetExplorerEvents(ie) 
  282.     >>> ie.Visible=1
  283.     Visibility changed:  1
  284.     >>>
  285.     '''
  286.     clsid = str(pywintypes.IID(clsid))
  287.     return gencache.GetClassForCLSID(clsid).default_source
  288.  
  289.  
  290. def Record(name, object):
  291.     '''Creates a new record object, given the name of the record,
  292.   and an object from the same type library.
  293.  
  294.   Example usage would be:
  295.     app = win32com.client.Dispatch("Some.Application")
  296.     point = win32com.client.Record("SomeAppPoint", app)
  297.     point.x = 0
  298.     point.y = 0
  299.     app.MoveTo(point)
  300.   '''
  301.     import gencache
  302.     object = gencache.EnsureDispatch(object._oleobj_)
  303.     module = sys.modules[object.__class__.__module__]
  304.     package = gencache.GetModuleForTypelib(module.CLSID, module.LCID, module.MajorVersion, module.MinorVersion)
  305.     
  306.     try:
  307.         struct_guid = package.RecordMap[name]
  308.     except KeyError:
  309.         raise ValueError, "The structure '%s' is not defined in module '%s'" % (name, package)
  310.  
  311.     return pythoncom.GetRecordFromGuids(module.CLSID, module.MajorVersion, module.MinorVersion, module.LCID, struct_guid)
  312.  
  313. _PyIDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]
  314. from types import TupleType
  315. from pywintypes import UnicodeType
  316.  
  317. class DispatchBaseClass:
  318.     
  319.     def __init__(self, oobj = None):
  320.         if oobj is None:
  321.             oobj = pythoncom.new(self.CLSID)
  322.         elif type(self) == type(oobj):
  323.             
  324.             try:
  325.                 oobj = oobj._oleobj_.QueryInterface(self.CLSID, pythoncom.IID_IDispatch)
  326.             except pythoncom.com_error:
  327.                 details = None
  328.                 import winerror
  329.                 if details[0] != winerror.E_NOINTERFACE:
  330.                     raise 
  331.                 
  332.                 oobj = oobj._oleobj_
  333.  
  334.         
  335.         self.__dict__['_oleobj_'] = oobj
  336.  
  337.     
  338.     def __repr__(self):
  339.         
  340.         try:
  341.             mod_doc = sys.modules[self.__class__.__module__].__doc__
  342.             if mod_doc:
  343.                 mod_name = 'win32com.gen_py.' + mod_doc
  344.             else:
  345.                 mod_name = sys.modules[self.__class__.__module__].__name__
  346.         except KeyError:
  347.             mod_name = 'win32com.gen_py.unknown'
  348.  
  349.         return '<%s.%s>' % (mod_name, self.__class__.__name__)
  350.  
  351.     
  352.     def __cmp__(self, other):
  353.         other = getattr(other, '_oleobj_', other)
  354.         return cmp(self._oleobj_, other)
  355.  
  356.     
  357.     def _ApplyTypes_(self, dispid, wFlags, retType, argTypes, user, resultCLSID, *args):
  358.         return self._get_good_object_(apply(self._oleobj_.InvokeTypes, (dispid, 0, wFlags, retType, argTypes) + args), user, resultCLSID)
  359.  
  360.     
  361.     def __getattr__(self, attr):
  362.         args = self._prop_map_get_.get(attr)
  363.         if args is None:
  364.             raise AttributeError, "'%s' object has no attribute '%s'" % (repr(self), attr)
  365.         
  366.         return apply(self._ApplyTypes_, args)
  367.  
  368.     
  369.     def __setattr__(self, attr, value):
  370.         if self.__dict__.has_key(attr):
  371.             self.__dict__[attr] = value
  372.             return None
  373.         
  374.         
  375.         try:
  376.             (args, defArgs) = self._prop_map_put_[attr]
  377.         except KeyError:
  378.             raise AttributeError, "'%s' object has no attribute '%s'" % (repr(self), attr)
  379.  
  380.         apply(self._oleobj_.Invoke, args + (value,) + defArgs)
  381.  
  382.     
  383.     def _get_good_single_object_(self, obj, obUserName = None, resultCLSID = None):
  384.         if _PyIDispatchType == type(obj):
  385.             return Dispatch(obj, obUserName, resultCLSID, UnicodeToString = NeedUnicodeConversions)
  386.         elif NeedUnicodeConversions and UnicodeType == type(obj):
  387.             return str(obj)
  388.         
  389.         return obj
  390.  
  391.     
  392.     def _get_good_object_(self, obj, obUserName = None, resultCLSID = None):
  393.         if obj is None:
  394.             return None
  395.         elif type(obj) == TupleType:
  396.             return tuple(map((lambda o, s = self, oun = obUserName, rc = resultCLSID: s._get_good_single_object_(o, oun, rc)), obj))
  397.         else:
  398.             return self._get_good_single_object_(obj, obUserName, resultCLSID)
  399.  
  400.  
  401.